package com.qiudaozhang.controller;

import com.qiudaozhang.dto.ResponseCode;
import com.qiudaozhang.model.StgDAccountRecord;
import com.qiudaozhang.model.User;
import com.qiudaozhang.model.UserRecharge;
import com.qiudaozhang.service.StgDAccountRecordService;
import com.qiudaozhang.service.UserRechargeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("bank")
public class BankController {
    @Autowired
    private UserRechargeService userRechargeService;
    @Autowired
    private StgDAccountRecordService stgDAccountRecordService;


    @InitBinder
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), false));
    }

    @RequestMapping("to/recharge")
    public String toRecharge(Model model){
        String rechargeNum =userRechargeService.findNowCode();
        if(rechargeNum==null||"".equals(rechargeNum)){
            rechargeNum ="1233400000001";
        }else{
            Long num = new Long(rechargeNum)+1;
            rechargeNum= num.toString();
        }
        model.addAttribute("rechargeNum",rechargeNum);
        return "bank/recharge";
    }

    @RequestMapping("chongZhi")
    public String recharge(UserRecharge userRecharge, HttpSession session,
                           Model model, @Param("pwd") String pwd){
        ResponseCode code=new ResponseCode();
        User user= (User) session.getAttribute("sessionUser");
        if(user.getPassword2().equals(pwd)){
            code = userRechargeService.recharge(userRecharge,user);
        }else{
            code.setCode(ResponseCode.FAIL);
            code.setMsg("密码错误");
        }
        model.addAttribute("code",code);
        return  "redirect:to/recharge";
    }

    @RequestMapping("to/account")
    public String toAccount(){
        return "bank/account";
    }
    @RequestMapping("account")
    @ResponseBody
    public ResponseCode account(@RequestParam(value = "limit") Integer pageSize,
                                @RequestParam(value = "page") Integer pageNum,
                                @Param("time1")String time1,
                                @Param("time2") String time2){
        ResponseCode code = stgDAccountRecordService.query(pageNum,pageSize,time1,time2);
        return code;
    }
}
